package Parser;

import java.util.HashMap;

import model.CharacterParameters;
import model.Difficulty;
import model.GWParameters;
import model.Grid;
import model.ObjectRepresentation;
import model.SGrid;
import model.Sound;
import model.Sprite;

import org.jdom2.Element;

public class GenerateGWParameters {

	public GenerateGWParameters(GWParameters gwParameters) {

		ParserJDOM parserJDOM = new ParserJDOM();
		parserJDOM.parse("res/jeu1/configuration.xml");
		Element root = parserJDOM.getRoot();
		gwParameters.setDeviceBackgroundPath(root.getChild(
				"DeviceBackgroundPath").getValue());
		gwParameters.setGridBackgroundPath(root.getChild("GridBackgroundPath")
				.getValue());

		Sound sound = new Sound();
		sound.setPath(root.getChild("GameTickSound").getValue());

		Grid grid = new Grid();
		parserJDOM.resetRoot();
		Element element = parserJDOM.searchElement("GGrid");
		grid.setColsNumber(Integer.valueOf(element.getChild("colsNumber")
				.getValue()));
		grid.setRowsNumber(Integer.valueOf(element.getChild("rowsNumber")
				.getValue()));
		gwParameters.setGgrid(grid);

		parserJDOM.resetRoot();
		Element element2 = parserJDOM.searchElement("Difficulty");
		Difficulty difficulty = new Difficulty();
		difficulty.setEnemyAppearanceInterval(Double.parseDouble(element2
				.getChild("enemyAppearanceInterval").getValue()));
		difficulty.setEnemyAppearanceIntervalDecrementation(Double
				.parseDouble(element2.getChild(
						"enemyAppearanceIntervalDecrementation").getValue()));
		difficulty.setGameTickInterval(Double.parseDouble(element2.getChild(
				"gameTickInterval").getValue()));

		SGrid sGrid = new SGrid();
		CharacterParameters characterParameters = new CharacterParameters();
		CharacterParameters enemyParameters = new CharacterParameters();

		new GenerateSGrid(parserJDOM, sGrid);
		new GenerateCharactereParameters(parserJDOM, characterParameters);
		new GenerateEnemyParameters(parserJDOM, enemyParameters);

		gwParameters.setSgrid(sGrid);
		gwParameters.setCharacterParameters(characterParameters);
		gwParameters.setEnemyParametres(enemyParameters);

		HashMap<String, ObjectRepresentation> map = new HashMap<String, ObjectRepresentation>();

		if (characterParameters.getMovementSpriteSequence() != null) {
			map.put(characterParameters.getMovementSpriteSequence().getId(),
					characterParameters.getMovementSpriteSequence());
			for (Sprite sprite : characterParameters
					.getMovementSpriteSequence().getSprites()) {
				map.put(sprite.getId(), sprite);
			}
		}

		if (enemyParameters.getMovementSpriteSequence() != null) {
			map.put(enemyParameters.getMovementSpriteSequence().getId(),
					enemyParameters.getMovementSpriteSequence());
			for (Sprite sprite : enemyParameters.getMovementSpriteSequence()
					.getSprites()) {
				map.put(sprite.getId(), sprite);
			}
		}

		gwParameters.setRepresentations(map);

	}
}
